﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataBase.Daihoctuonglai;
using System.Data;
using Dhtl_Repositry.UnitOfWork;

namespace Dhtl_Repositry.Repository
{
    public class UserRepositry : RepositoryBase<Usser>
    {

        public UserRepositry()
            : base()
        {
        }

        public UserRepositry(WorkUnit unit)
            : base(unit)
        {
        }

        public override int CreateOrUpdate(Usser entity)
        {
            try
            {
                Context.Entry(entity).State = entity.Id == 0 ?
                                       EntityState.Added :
                                       EntityState.Modified;
                Context.SaveChanges();
                return entity.Id;
            }
            catch { return 0; }
        }

        public override string Delete(int entityId)
        {
            try
            {
                var entity = Context.Ussers.SingleOrDefault(o => o.Id == entityId);
                Context.Ussers.Remove(entity);
                Context.SaveChanges();
                return "success";
            }
            catch { return "error"; }
        }

        public string DeleteByName(string entityId)
        {
            try
            {
                var entity = Context.Ussers.SingleOrDefault(o => o.Email == entityId);
                Context.Ussers.Remove(entity);
                Context.SaveChanges();
                return "success";
            }
            catch { return "error"; }
        }

        public override Usser GetById(int entityId)
        {
            try
            {
                return Context.Ussers.SingleOrDefault(p => p.Id == entityId);
            }
            catch { return null; }
        }

        public override List<Usser> List()
        {
            try
            {
                return Context.Ussers.OrderByDescending(p => p.Id).ToList();
            }
            catch { return null; }
        }

        
        public override List<Usser> GetTableListById(int entityId)
        {
            try
            {
                var c = from i in Context.Ussers
                        join k in Context.UserInRoles on i.Id equals k.UserId
                        join l in Context.Roles on k.RolesId equals l.Id
                        where (k.Id == entityId || entityId == 0)
                        select i;
                return c.ToList();
            }
            catch { return null; }
        }

        #region Function


        public bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            try
            {
                var entityUser = Context.Ussers.First(k => k.Email == username && k.Pass == oldPassword);
                entityUser.Pass = newPassword;
                Context.SaveChanges();
                return true;
            }
            catch { return false; }
        }


        public Usser CreateUser(string username, string password, string email)
        {
            try
            {
                Usser entityUser = new Usser();
                entityUser.Email = username;
                entityUser.Pass = password;
                entityUser.Email = email;
                Context.Ussers.Add(entityUser);
                Context.SaveChanges();
                return entityUser;
            }
            catch { return null; }
        }


        public bool DeleteUserByUserName(string userName)
        {
            try
            {
                var entityUser = Context.Ussers.SingleOrDefault(k => k.Email == userName);
                Context.Ussers.Remove(entityUser);
                Context.SaveChanges();
                return true;
            }
            catch { return false; }
        }

        public Usser GetUserByEmail(string email)
        {
            try
            {
                var entityUser = Context.Ussers.SingleOrDefault(k => k.Email == email);
                return entityUser;
            }
            catch { return null; }
        }

        public Usser LogIn(string username, string Password)
        {
            try
            {
                var entityUser = Context.Ussers.SingleOrDefault(k => k.Email == username && k.Pass == Password);
                if (entityUser != null)
                    return entityUser;
                else return null;
            }
            catch { return null; }
        }

        public bool checkStatusUser(string username, string Password)
        {
            try
            {
                var entityUser = Context.Ussers.SingleOrDefault(k => k.Email == username && k.Pass == Password);
                if (entityUser != null)
                    return true;
                else return false;
            }
            catch { return false; }
        }

        public string[] GetRolesForUser(string username)
        {
            var c = (from i in Context.Ussers
                    join k in Context.UserInRoles on i.Id equals k.UserId
                    join l in Context.Roles on k.RolesId equals l.Id
                     where i.Email == username
                    select l.Name).ToArray();
            return c;
        }

        public string[] GetUsersInRole(string rolesName)
        {
            var c = (from i in Context.Ussers
                     join k in Context.UserInRoles on i.Id equals k.UserId
                     join l in Context.Roles on k.RolesId equals l.Id
                     where l.Name == rolesName
                     select i.Email).ToArray();
            return c;
        }


        public bool checkUserInroles(string UserName, string RolesName)
        {
            try
            {
                var c = (from i in Context.Ussers
                         from j in Context.UserInRoles
                         from k in Context.Roles
                         where i.Id == j.Id && j.RolesId == k.Id
                         select i);
                if (c != null) return true;
                else return false;
            }
            catch { return false; }
        }

        public string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            var c = (from i in Context.Ussers
                     join k in Context.UserInRoles on i.Id equals k.UserId
                     join l in Context.Roles on k.RolesId equals l.Id
                     where l.Name == roleName && i.Email == usernameToMatch
                     select i.Email).ToArray();
            return c;
        }


        #endregion

    }
}
